package com.example2016.cjj.servicedemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example2016.cjj.servicedemo.MyDownloader.DownloadService;
import com.example2016.cjj.servicedemo.MyDownloader.DownloadServiceProgressListener;

public class DownloadActivity extends AppCompatActivity implements View.OnClickListener {

    private  DownloadService.DownloadServiceBinder downloadServiceBinder;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadServiceBinder =(DownloadService.DownloadServiceBinder)service;

            downloadServiceBinder.getService().setDownloadServiceProgressListener(new DownloadServiceProgressListener() {
                @Override
                public void onProgressChanged(int progressValue) {
                    progressTextView.setText(progressValue+"%");
                    progressBar.setProgress(progressValue);
                }
            });


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private EditText downloadUrlTV;

    private TextView progressTextView;

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        Button startDownload = (Button) findViewById(R.id.btn_startDownload);
        startDownload.setOnClickListener(this);
        Button pauseDownload = (Button) findViewById(R.id.btn_pauseDownload);
        pauseDownload.setOnClickListener(this);
        Button cancelDownload = (Button) findViewById(R.id.btn_cancelDownload);
        cancelDownload.setOnClickListener(this);

        downloadUrlTV = (EditText) findViewById(R.id.et_downloadUrl);
        progressBar= (ProgressBar) findViewById(R.id.progressbar);
        progressTextView = (TextView) findViewById(R.id.progressTextView);

        Intent serviceIntent = new Intent(this, DownloadService.class);
        startService(serviceIntent); //启动下载服务
        bindService(serviceIntent,serviceConnection,BIND_AUTO_CREATE); //绑定下载服务

    }

    @Override
    public void onClick(View v) {
        if(downloadServiceBinder ==null){
            return;
        }

        switch (v.getId()){
            case R.id.btn_startDownload:
                //启动下载服务去下载
                //String url = "http://sw.bos.baidu.com/sw-search-sp/software/d222f2cb25382/codeblocks_16.1.0.0_setup.exe";
                String url = downloadUrlTV.getText().toString();

                downloadServiceBinder.startDownload(url);
                break;
            case R.id.btn_pauseDownload:
                downloadServiceBinder.pauseDownload();
                break;
            case R.id.btn_cancelDownload:
                downloadServiceBinder.cancelDownload();
                break;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}
